/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_priority_queue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/**
  * @file        jh_priority_queue.c
  * @brief       优先队列
  * @details     优先队列
  * @author      Hong Jiahua
  * @version     1.1.1
  * @date        2022-02-20
  * @copyright   2022 Hong Jiahua MIT License
  */
#include "jh_priority_queue.h"

/************************************************************************/
/*                                                                      */
/************************************************************************/
/**
  * @brief      判断优先队列是否为空
  * @param[in]  pqueue  优先队列
  *
  * @return     errcode
  * @retval      1      优先队列为空
  * @retval      0      优先队列不为空
  * @retval     -1      失败
  */
int jh_priority_queue_is_empty(jh_priority_queue_t* pqueue) {
    if(pqueue == NULL)
        return -1;
    
    return pqueue->data_len == 0;
}

/**
  * @brief      判断优先队列是否已满
  * @param[in]  pqueue  优先队列
  *
  * @return     errcode
  * @retval      1      优先队列已满
  * @retval      0      优先队列未满
  * @retval     -1      失败
  */
int jh_priority_queue_is_full(jh_priority_queue_t* pqueue) {
    if(pqueue == NULL)
        return -1;
    
    return pqueue->data_len == (pqueue->num - 1);
}

/**
  * @brief      获取优先队列的数据数量
  * @param[in]  pqueue  优先队列
  *
  * @return     errcode
  * @retval     >=0     优先队列的数据数量
  */
size_t jh_priority_queue_count(jh_priority_queue_t* pqueue) {
    if(pqueue == NULL)
        return 0;
    
    return pqueue->data_len;
}

/**
  * @brief      优先队列入队
  * @param[in]  pqueue  优先队列
  * @param[in]  item    要入队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_priority_queue_push(jh_priority_queue_t* pqueue, void* item) {
    if(pqueue == NULL || item == NULL || (pqueue->data_len + 1) >= pqueue->num || pqueue->compare == NULL)
        return -1;

    int i = ++pqueue->data_len;
    for (; i > 1 && pqueue->compare(item, pqueue->base + (i >> 1) * pqueue->size); i >>= 1) {
        memcpy(pqueue->base + i * pqueue->size, pqueue->base + (i >> 1) * pqueue->size, pqueue->size);
    }
    memcpy(pqueue->base + i * pqueue->size, item, pqueue->size);
    
    return 0;
}

/**
  * @brief      优先队列出队
  * @param[in]  pqueue  优先队列
  * @param[in]  item    要出队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_priority_queue_pop(jh_priority_queue_t* pqueue, void* item) {
    void *tmp;
    int i = 1, j = 2;
    
    if(pqueue == NULL || item == NULL || pqueue->data_len == 0 || pqueue->compare == NULL)
        return -1;
    
    tmp = pqueue->base + pqueue->data_len * pqueue->size;
    memcpy(pqueue->base, pqueue->base + pqueue->size, pqueue->size);
    memcpy(pqueue->base + pqueue->size, pqueue->base + pqueue->data_len * pqueue->size, pqueue->size);
    
    pqueue->data_len --;
    while (j <= pqueue->data_len) {
        if (j != pqueue->data_len && pqueue->compare(pqueue->base + (j + 1) * pqueue->size, pqueue->base + j * pqueue->size)) {
            j ++;
        }
        
        if(!pqueue->compare(pqueue->base + j * pqueue->size, tmp)) {
            break;
        }
        
        memcpy(pqueue->base + i * pqueue->size, pqueue->base + j * pqueue->size, pqueue->size);
        i = j;
        j <<= 1;
    }
    memcpy(pqueue->base + i * pqueue->size, tmp, pqueue->size);
    memcpy(item, pqueue->base, pqueue->size);
    
    return 0;
}

/**
  * @brief      优先队列查看队首的数据
  * @param[in]  pqueue  优先队列
  * @param[in]  item    队首的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_priority_queue_peek(jh_priority_queue_t* pqueue, void* item) {
    if(pqueue == NULL || item == NULL || pqueue->data_len == 0)
        return -1;
    
    memcpy(item, pqueue->base + pqueue->size, pqueue->size);
    
    return 0;
}

/**
  * @brief      清空优先队列
  * @param[in]  pqueue  优先队列
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_priority_queue_clear(jh_priority_queue_t* pqueue) {
    if(pqueue == NULL)
        return -1;
    
    pqueue->data_len = 0;
    
    return 0;
}

/**
  * @brief      优先队列初始化
  * @param[in]  pqueue  优先队列
  * @param[in]  base    指向用于优先队列的数组
  * @param[in]  num     数组中能存放的数据项数量
  * @param[in]  size    数组中每个数据项的大小
  * @param[in]  compare 指向用于比较两个数据项的函数指针
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_priority_queue_init(jh_priority_queue_t* pqueue, void* base, size_t num, size_t size, int (*compare)(const void*, const void*)) {
    if(pqueue == NULL || base == NULL || compare == NULL)
        return -1;
    
    pqueue->base = base;
    pqueue->num = num;
    pqueue->size = size;
    pqueue->compare = compare;
    pqueue->data_len = 0;
    
    return 0;
}
